package delegate4j;

import com.sun.istack.internal.Nullable;

import java.util.ArrayList;
import java.util.concurrent.FutureTask;

public class Action1<T> extends Delegate<IAction1<T>> {
    public Action1(IAction1<T> delegate) {
        super(delegate);
    }

    public void invoke(T t) throws Exception {
        for (IAction1<T> action : delegates) {
            action.invoke(t);
        }
    }

    public AsyncResult<Void> invokeAsync(T t, @Nullable AsyncDelegateCallback callback) {
        ArrayList<FutureTask<Void>> tasks = new ArrayList<>();
        for (int i = 0; i < delegates.size(); i++) {
            tasks.add(delegates.get(i).invokeAsync(t, null));
        }

        waitTasksFinish(tasks, new Object[]{t}, callback);

        //返回tasks的浅拷贝
        return new AsyncResult<>(tasks);
    }
}
